package isstra.gestore;

import java.util.concurrent.locks.*;

public class GestoreLC implements Gestore{
	private boolean release=false;
	private int cnt=0;
	private Lock lock=new ReentrantLock();
	private Condition attesa=lock.newCondition();
	public void request(){
		lock.lock();
		try{
			while( release ){
				try{ attesa.await(); }catch(InterruptedException e){}
			}
			cnt++;
			if( cnt==3 ) release=true;
			System.out.println(Thread.currentThread().getName()+" entra");
		}finally{
			lock.unlock();
		}
	}//request
	public void finish(){
		lock.lock();
		try{
			cnt--;
			if( cnt==0 ){
				release=false;
				attesa.signalAll();
			}
			System.out.println(Thread.currentThread().getName()+" esce");
		}finally{
			lock.unlock();
		}
	}//finish
}//GestoreLC
